﻿using System;
using System.Collections.Generic;
using System.Linq;
using Todo.Entity.Models;

namespace Todo.Logic.Server
{
    public class InventoryServer
    {
        private TodoContext _ctx = new TodoContext();
        List<Inventory> _inventorys = new List<Inventory>();
        List<Things> _things = new List<Things>();

        ///// <summary>
        ///// 判断是否有重复的清单ID
        ///// </summary>
        ///// <param name="inventoryId"></param>
        ///// <returns></returns>
        //public bool JudgeInventoryIdRepeat(int inventoryId)
        //{
        //    return _ctx.Inventory.Any(item => item.Id == inventoryId) ? false : true;
        //}

            /// <summary>
            /// 判断该清单是否为该用户所有
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="inventoryId"></param>
            /// <returns></returns>
         public bool belongTo(int userId,int inventoryId)
        {
            return _ctx.Inventory.Any(item => item.Id == inventoryId && item.UserId == userId)?true:false;
        }

        /// <summary>
        /// 创建一个清单
        /// </summary>
        /// <param name="user"></param>
        /// <param name="createrName"></param>
        /// <param name="listName"></param>
        /// <param name="inventoryId"></param>
        public void CreateInventory(int userId, string listName)
        {
            Inventory inventory = new Inventory
            {
                CreateionTime = DateTime.Now,
                Name = listName,
                UserId = userId,
            };
            _ctx.Inventory.Add(inventory);
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 获取一个清单
        /// </summary>
        /// <param name="user"></param>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public Inventory GetInventory(int userId, int inventoryId)
        {
            _inventorys = _ctx.Inventory.Where(item => item.UserId == userId).ToList();
            var result = _inventorys.SingleOrDefault
                (inventory => inventory.Id == inventoryId);
            return result;
        }

        /// <summary>
        /// 修改清单名称
        /// </summary>
        /// <param name="user"></param>
        /// <param name="newName"></param>
        /// <param name="inventory"></param>
        /// <returns></returns>
        public void ChangeName(string newName, Inventory inventory)
        {
            inventory.Name = newName;
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 查看清单所有事项
        /// </summary>
        /// <param name="inventory"></param>
        /// <returns></returns>
        public List<Things> ViewAllThingInList(int inventoryId)
        {
            _things = _ctx.Things.Where(item => item.InventoryId == inventoryId).ToList();
            if (_things != null)
            {
                return _things;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除清单
        /// </summary>
        /// <param name="inventoryId"></param>
        public void RemoveInventory(int inventoryId)
        {
            Inventory inventory= _ctx.Inventory.SingleOrDefault(item => item.Id == inventoryId);
            _ctx.Inventory.Remove(inventory);
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 获取所有清单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Inventory> GetViewAllInventoryt(int userId)
        {
            return _ctx.Inventory.Where(item => item.UserId == userId).ToList();
        }
    }

}
